import Vue from 'vue'
import App from './App'
import cookies from 'weapp-cookie'

import router from './router/index.js'
import store from './store/index.js'
import permission from './utils/permission'

import { isEmpty } from './utils/types'
import { guid } from './utils/lib'
import { STATIC_URL_FN, appVersion } from './config.js'

/* import uniui from '@dcloudio/uni-ui';

// 引入 uni-ui 组件库
Vue.use(uniui); */

import uniui from '@dcloudio/uni-ui';

Vue.config.productionTip = false

Vue.use(router)

Vue.prototype.$isEmpty = isEmpty

Vue.prototype.$auth = permission

Vue.prototype.$guid = guid


/**
 * Toast
 */
Vue.prototype.$toast = (msg, duration = 3000) => {
	// #ifdef APP-PLUS
	const richtext = '<font color="#FFFFFF" style="font-size:14px;">' + msg + '</font>' 
	plus.nativeUI.toast(richtext, {
		background: 'rgba(0,0,0,1)',
		duration: duration > 3000 ? 'long' : 'short',
		type: 'richtext',
		verticalAlign: 'top'
	})
	// #endif
	// #ifndef APP-PLUS
	uni.showToast({
		position: 'top',
		title: msg,
		icon: 'none',
		duration: duration
	})
	// #endif
}

/**
 * 返回并刷新上一页内容
 */
Vue.prototype.$refreshAndToPrev = (that, toPrev = true) => {
	const pages = getCurrentPages()
	if (pages.length >= 2) {
		const prevPage = pages[pages.length - 2]
		
		if (prevPage.$vm) {
			prevPage.$vm.refreshPage = true
		} else {
			prevPage.refreshPage = true
		}
	}
	if (toPrev) {
		that.$nextTick(function() {
			that.$Router.navigateBack()
		})
	}
}

/**
 * 拼接静态资源路径
 * @param {Object} val
 * @return {String}
 */
Vue.prototype.$static = STATIC_URL_FN
Vue.filter('static', Vue.prototype.$static)

Vue.prototype.$clearStorage = () => {
	const username = uni.getStorageSync('username') || null
	const quickConfig = uni.getStorageSync('quickConfig') || [1, 2, 3, 4]
	const appid = uni.getStorageSync('appid') || null
	uni.clearStorageSync()
	if (username) {
		uni.setStorageSync('username', username)
	}
	if (appid) {
		uni.setStorageSync('appid', appid)
	}
	uni.setStorageSync('version', appVersion)
	uni.setStorageSync('quickConfig', quickConfig)
}

// 校验角色
Vue.prototype.checkRole = function(role) {
	let roleStr = this.$store.getters['user/curRoleFlag'];
	return roleStr[role]?true:false
}

App.mpType = 'app'

if (process.env.NODE_ENV !== 'development') {
	console.log = () => {}
}

const app = new Vue({
	...App,
	store
})

app.$mount()

export default app
